package org.smile.auth.request;

import org.smile.auth.session.Session;
import org.smile.log.LoggerHandler;
import org.smile.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class ServletRequest implements Request{
    static String Bearer="Bearer";
    HttpServletRequest request;
    String authorization="Authorization";
    public ServletRequest(HttpServletRequest request){
        this.request=request;
    }
    @Override
    public String getSessionId() {
        return request.getHeader("sessionId");
    }

    @Override
    public String getToken() {
        String authString= request.getHeader(getAuthorization());
        LoggerHandler.logger.debug(request.getRequestURL()+" "+authString);
        if (StringUtils.isBlank(authString)) {
            return null;
        }
       authString= authString.trim();
        if (StringUtils.notBlank(authString)) {
            if(authString.startsWith(Bearer)){
                String token= StringUtils.substringAfter(authString,Bearer);
                return StringUtils.trim(token);
            }else{
                return StringUtils.trim(authString);
            }
        }
        return null;
    }

    @Override
    public String getAuthorization() {
        return this.authorization;
    }

    public void setAuthorization(String authorization) {
        this.authorization = authorization;
    }
}
